import { useEffect, useState } from "react";
import { getBreadcRumb, push } from "@/utils/util";
import ListForm from '@/components/ListForm'
import { useLocation, useParams } from "react-router";
import { save, update, detail } from "@/apis/playWith";
import { message } from "antd";
function PlayWithManageCreate() {
    const params = useParams()
    const location = useLocation()
    const [values, setvalues] = useState<any>()
    const text = location.pathname.search(/edit/) != -1 ? "编辑" : params.id ? '查看' : '新增'
    getBreadcRumb([{ name: "陪玩列表", path: "/playWithList" }, { name: text + '陪玩' }])
    const list: listItem[] = [
        {
            name: "playWithName",
            label: "陪玩名称",
            type: 'input',
            rules: [{ required: true, message: '请输入陪玩名称' }],
            disabled: text == '查看'
        },
        {
            name: "unitPrice",
            label: "单价",
            type: "input",
            rules: [{ required: true, message: "请输入陪玩单价" }],
            disabled: text == '查看'
        },
        {
            name: "commissionRate",
            label: "抽成",
            type: "inputNumber",
            min: 0,
            max: 100,
            rules: [{ required: true, message: "请输入抽成" }],
            disabled: text == '查看'
        },
        {
            name: "mgtType",
            label: "管理类型",
            type: "radio",
            initialValue: 2,
            options: [{ label: '管理陪玩', value: 1 }, { label: '普通陪玩', value: 2 }],
            rules: [{ required: true, message: "请选择管理类型" }],
            disabled: text == '查看'
        },
    ]
    //确定调接口
    async function onFinish(values: playWithParams) {
        if (text == '新增') {
            await save(values)
            message.success('新增陪玩成功')
        }
        if (text == '编辑') {
            if (params.id) {
                await update({ ...values, playWithId: params.id })
                message.success('编辑陪玩成功')
            }
        }
        push('/playWithList')
    }
    async function init() {
        if (params.id) {
            let res = await detail({ playWithId: params.id })
            setvalues({ ...res.data })
        }
    }
    useEffect(() => {
        init()
    }, [])
    return (
        <div className='tableWrap'>
            <h2>{text}陪玩</h2>
            <ListForm list={list} values={values} onFinish={onFinish}></ListForm>
        </div>
    )
}

export default PlayWithManageCreate;